package com.blog.service.service;

import com.blog.check.check.CheckResult;
import com.blog.check.check.CommentCheck;
import com.blog.check.result.Result;
import com.blog.check.resultenum.CommentChkRsEnum;
import com.blog.dao.domain.BlogDO;
import com.blog.dao.domain.CommentDO;
import com.blog.dao.query.CommentQuery;
import com.blog.exception.ServiceException;
import com.blog.form.CommentForm;
import com.blog.service.dto.CommentDTO;
import com.blog.until.BeanCopierUtil;
import com.blog.until.PrimaryKeyUtil;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class CommentService extends BaseService{

    private CommentCheck commentCheck;

    @Autowired
    public void setCommentCheck(CommentCheck commentCheck){
        this.commentCheck = commentCheck;
    }

    @Transactional(rollbackFor = Exception.class)
    public Result list(CommentQuery commentQuery){
        try{

            List<CommentDTO> commentDTOList = new ArrayList<>();
            List<CommentDO> commentDOList = commentDAO.list(commentQuery);

            if(commentDOList.size() == 0){
                return Result.success(commentDTOList);
            }

            for (CommentDO commentDO:commentDOList){
                CommentDTO commentDTO = getShowDTO(commentDO);
                commentDTOList.add(commentDTO);
            }

            return Result.success(commentDTOList);
        }catch (Exception e){
            throw new ServiceException(e);
        }
    }

    private CommentDTO getShowDTO(CommentDO commentDO) {
        CommentDTO commentDTO = new CommentDTO();
        commentDTO.setId(commentDO.getId());
        commentDTO.setUsername(commentDTO.getUsername());
        commentDTO.setBlogId(commentDO.getBlogId());
        commentDTO.setContent(commentDO.getContent());
        commentDTO.setCommentDate(commentDO.getCommentDate());
        return commentDTO;
    }

    @Transactional(rollbackFor = Exception.class)
    public Result save(CommentForm commentForm){

        try{

            CheckResult checkResult = commentCheck.checkSave(commentForm);
            if (checkResult.getCode() != CommentChkRsEnum.SUCCESS.getCode()){
                logger.error(logInfo(checkResult));
                return Result.failure(checkResult);
            }

            CommentDO commentDO = buildSaveComment(commentForm);
            commentDAO.save(commentDO);
            return Result.success();
        }catch (Exception e){
            throw new ServiceException(e);
        }

    }

    @SneakyThrows
    private CommentDO buildSaveComment(CommentForm commentForm) {
        CommentDO commentDO = BeanCopierUtil.create(commentForm,CommentDO.class);
        commentDO.setId(PrimaryKeyUtil.get());
        commentDO.setUsername(getUsername());
        commentDO.setBlogId(commentForm.getBlogId());
        commentDO.setContent(commentDO.getContent());
        commentDO.setCommentDate(getTime());
        BlogDO blogDO = blogDAO.get(commentForm.getBlogId());
        blogDO.setReplyHit(blogDO.getReplyHit() + 1);
        blogDAO.update(blogDO);
        return commentDO;
    }

    @Transactional(rollbackFor = Exception.class)
    public Result remove(String commentId){
        try{
            CheckResult checkResult = commentCheck.checkRemove(commentId);
            if (checkResult.getCode() != CommentChkRsEnum.SUCCESS.getCode()){
                logger.error(logInfo(checkResult));
                return Result.failure(checkResult);
            }
            commentDAO.remove(commentId);
            return Result.success();
        }catch (Exception e){
            throw new ServiceException(e);
        }
    }

}
